package com.yunji.order.service;

import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.order.dao.IRequestDao;
import com.yunji.order.domain.RequestDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.order.service.impl
 * @Title: RequestServiceImpl
 * @Description: 请求接口实现
 * History:
 * Date						Version			Author			Summary
 * ========================================================================
 * 2024-04-30 16:57:39		V1.0			HuaAo		请求接口实现
 */
@Service
public class RequestService {
    private static final Logger logs = LoggerFactory.getLogger(RequestService.class);
    @Resource
    IRequestDao requestDao;


    public boolean createRequest(RequestDO request) {
        return requestDao.save(request);
    }


    public boolean updateRequest(RequestDO request) {
        return requestDao.updateById(request);
    }

    public boolean updateRequest(String tradeCode, Integer tradeType, String callbackCode, String callbackContent) {
        try {
            List<RequestDO> list = requestDao.listByCodeType(tradeCode, tradeType);
            if (BeanUtils.isNotNull(list)) {
                RequestDO request = list.get(0);
                RequestDO update = new RequestDO();
                update.setRequestId(request.getRequestId());
                update.setCallbackCode(callbackCode);
                update.setCallbackContent(callbackContent);
                requestDao.updateById(request);
            }
        } catch (Exception e) {
            logs.error("记录请求日志失败", e);
        }
        return false;
    }

}